<?php


namespace app\system\controller\vod;

use app\common\model\vod\CateModel;
use app\common\model\vod\VideoModel;
use app\system\controller\BaseController;

class IndexController extends BaseController
{
  /**
   * 无需登录方法
   * @var array
   */
  protected $noLogin = [];

  /**
   * 不检测权限方法
   * @var array
   */
  protected $noCheck = [];

  /**
   * 权限映射
   * @var array
   */
  protected $mapCheck = [
      '*' => 'index'
  ];
  /**
   * 视频模型
   * @var null
   */
  private $model = null;

  /**
   * 初始化
   */
  public function __construct()
  {
    parent::__construct();
    $this->model = new VideoModel();
  }

  /**
   * 视频列表
   * @return string|array
   * @throws \Exception
   */
  public function index()
  {
    if ($this->request->isAjax()) {
      $pageNumber = request()->param('pageNumber/d', 1); //当前页
      $pageSize   = request()->param('pageSize/d', 20); //每页多少条
      //排序
      $sortName  = request()->param('sortName', 'CreateTime'); //排序字段
      $sortOrder = ucfirst(request()->param('sortOrder', 'desc')); //升序降序
      $q['Sort'] = ['Field' => $sortName, 'Order' => $sortOrder];
      //分类检索
      $classId = $this->request->param('class_id/d');
      if (!empty($classId)) $q["ClassIds"] = [(int)$classId];
      //模糊检索
      $title = $this->request->param('title/s');
      if (!empty($title)) $q["Text"] = $title;
      //时间检索
      $startTime = $this->request->param('start_time');
      if (!empty($startTime)) {
        $startTime = date('Y-m-d\TH:i:s\Z', strtotime($startTime . ' 0:00:00'));
        $q['StartTime'] = $startTime;
      }
      $endTime = $this->request->param('end_time');
      if (!empty($endTime)) {
        $endTime = date('Y-m-d\TH:i:s\Z', strtotime($endTime . ' 23:59:59'));
        $q['EndTime'] = $endTime;
      }
      $data = $this->model->searchMedia($pageNumber, $pageSize, $q);
      return $data;
    }
    return $this->fetch();
  }

  /**
   * 修改视频信息
   * @return string
   * @throws \Exception
   */
  public function edit()
  {
    $cateModel = new CateModel();
    if ($this->request->isAjax()) {
      $data = $this->request->param();
      $this->validate($data, [
          'FileId|媒体ID' => 'require',
          'Name|视频标题' => 'require'
      ]);
      $this->model->edit($data);
      $this->success('操作成功');
    }
    $id   = $this->request->param('id');
    $info = $this->model->getVideoInfo($id);
    $cate = $cateModel->getList();
    $this->assign('info', $info);
    $this->assign('cate', $cate);
    return $this->fetch();
  }

  /**
   * 删除视频
   * @throws \Exception
   */
  public function del()
  {
    $id = $this->request->param('id');
    $this->model->del($id);
    $this->success('操作成功');
  }

  /**
   * 播放视频
   * @return string
   * @throws \Exception
   */
  public function play(){
    return $this->fetch();
  }
}